package com.wind.taobao.api.trade.itemprop.convert
{
	import com.wind.taobao.api.trade.itemprop.model.PropValue;
	import com.wind.taobao.core.util.TaobaoConstants;
	
	import mx.collections.ArrayCollection;


	/**
	 * Item PropValue转换器
	 * @author fxy
	 *
	 */
	public class PropValueConvert
	{
		/**
		 * 转换PropValue集合
		 */
		public static function convertPropValues(value:Object, type:String="json"):ArrayCollection
		{
			var propValues:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							propValues.addItem(convertPropValue((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return propValues;
		}

		/**
		 * 转换PropValue
		 */
		public static function convertPropValue(value:Object, type:String="json"):PropValue
		{
			var propValue:PropValue=new PropValue();
			if (value == null)
				return propValue;
			if (type == TaobaoConstants.XML)
			{
				propValue=convertXMLToPropValue(XML(value), propValue);
			}
			else
			{
				propValue=convertJSONToPropValue(value, propValue);
			}
			return propValue;
		}

		/**
		 * JSON转换为PropValue
		 */
		private static function convertJSONToPropValue(json:Object, propValue:PropValue):PropValue
		{
			if (json.hasOwnProperty(TaobaoConstants.CID))
			{
				propValue.cid=json[TaobaoConstants.CID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PID))
			{
				propValue.pid=json[TaobaoConstants.PID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PROP_NAME))
			{
				propValue.prop_name=json[TaobaoConstants.PROP_NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.VID))
			{
				propValue.vid=json[TaobaoConstants.VID];
			}
			if (json.hasOwnProperty(TaobaoConstants.NAME))
			{
				propValue.name=json[TaobaoConstants.NAME];
			}
			if (json.hasOwnProperty(TaobaoConstants.NAME_ALIAS))
			{
				propValue.name_alias=json[TaobaoConstants.NAME_ALIAS];
			}
			if (json.hasOwnProperty(TaobaoConstants.IS_PARENT))
			{
				propValue.is_parent=json[TaobaoConstants.IS_PARENT];//已转换Boolean
			}
			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				propValue.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.SORT_ORDER))
			{
				propValue.sort_order=json[TaobaoConstants.SORT_ORDER];
			}
			return propValue;
		}

		/**
		 * XML转换为PropValue
		 */
		private static function convertXMLToPropValue(value:XML, propValue:PropValue):PropValue
		{
			return propValue;
		}


	}
}